/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.app.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.wildfire.chat.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wildfire.chat.kit.AppService;

public class DiscoveryFragment extends Fragment {
    @BindView(R.id.webView)
    WebView webView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment_discovery, container, false);
        ButterKnife.bind(this, view);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setTextZoom(100);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void handleDiscovery(){
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http://")||url.startsWith("https://")){
                    view.loadUrl(url);
                    return true;
                }else if (url.startsWith("baidumap://")){
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return false;
            }

        });
        String discoveryUlr = AppService.Instance().getDiscoveryUlr();
        if (discoveryUlr!=null && !"".equals(discoveryUlr)){
            webView.loadUrl(discoveryUlr);
        }
//        else{
//            webView.loadUrl("https://gitee.com/");
//        }

    }

//    @OnClick(R.id.discovery)
//    void discovery() {
//        webView.loadUrl("https://www.baidu.com");
//    }
//
//    @OnClick(R.id.history)
//    void history() {
//        webView.goBack();
//    }

}
